package com.hy.study.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 能够作为key 进行排序的对象
 *
 * @author huyong
 * @date 2024 年 02 月 05 日 15:00: 45
 */
public class SortFlowBean implements WritableComparable<SortFlowBean> {
    /**
     * 上行流量
     */
    private long upFlow;
    /**
     * 下行流量
     */
    private long downFlow;
    /**
     * 总流量
     */
    private long sumFlow;

    public SortFlowBean(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow + downFlow;
    }

    public SortFlowBean() {
    }

    /**
     * 排序
     *
     * @param o the object to be compared.
     *
     * @return
     */
    @Override
    public int compareTo(SortFlowBean o) {
        int result = 0;
        if (this.sumFlow > o.sumFlow) {
            result = -1;
        } else if (this.sumFlow < o.sumFlow) {
            result = 1;
        }
        return result;
    }

    /**
     * 序列化
     *
     * @param out <code>DataOuput</code> to serialize this object into.
     *
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(this.upFlow);
        out.writeLong(this.downFlow);
        out.writeLong(this.sumFlow);
    }

    /**
     * 反序列化
     *
     * @param in <code>DataInput</code> to deseriablize this object from.
     *
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.sumFlow = in.readLong();
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public String toString() {
        return upFlow + "\t" + downFlow + "\t" + sumFlow;
    }
}
